package com.eliza.music.view.music.mine

import android.net.Uri
import android.os.Build
import android.provider.MediaStore.Downloads
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.core.app.ActivityOptionsCompat
import androidx.documentfile.provider.DocumentFile
import androidx.hilt.navigation.compose.hiltViewModel
import com.eliza.music.model.entity.BasicInfo
import com.eliza.music.model.entity.Song
import com.eliza.music.model.vm.PlayingMusicVm
import com.eliza.music.view.music.mine.MinePageX.PlaylistItem

@Composable
fun <T : BasicInfo> UserMusicPage(data: List<T>, modifier: Modifier = Modifier) {
    LazyColumn {
        items(data) {
            PlaylistItem(it)
        }
    }
}

@Composable
fun UserChooseFile(modifier: Modifier = Modifier) {
    var selectedUris by remember { mutableStateOf<List<Uri>>(emptyList()) }
//    var selectedUris by remember { mutableStateOf<Uri?>(null) }
//    rememberLauncherForActivityResult
    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) { it ->
            selectedUris = it
        }
    Column {
        Button(onClick = {
            launcher.launch("audio/*")
        }) {
            Text("Choose Files")
        }
        LazyColumn {
            items(selectedUris) { it ->
                Text(text = "scheme                    ${it.scheme}")
                Text(text = "schemeSpecificPart        ${it.schemeSpecificPart}")
                Text(text = "encodedSchemeSpecificPart ${it.encodedSchemeSpecificPart}")
                Text(text = "authority                 ${it.authority}")
                Text(text = "encodedAuthority          ${it.encodedAuthority}")
                Text(text = "userInfo                  ${it.userInfo}")
                Text(text = "encodedUserInfo           ${it.encodedUserInfo}")
                Text(text = "host                      ${it.host}")
                Text(text = "port                      ${it.port}")
                Text(text = "path                      ${it.path}")
                Text(text = "encodedPath               ${it.encodedPath}")
                Text(text = "query                     ${it.query}")
                Text(text = "encodedQuery              ${it.encodedQuery}")
                Text(text = "fragment                  ${it.fragment}")
                Text(text = "encodedFragment           ${it.encodedFragment}")
                Text(text = "pathSegments              ${it.pathSegments}")
                Text(text = "lastPathSegment           ${it.lastPathSegment}")
                Text(text = "queryParameterNames       ${it.queryParameterNames}")
            }
        }
    }
}

@Composable
fun ChooseFile(mimetypes: String): Uri? {
    var selectedUri by remember { mutableStateOf<Uri?>(null) }
    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { it ->
            selectedUri = it
        }
    launcher.launch(mimetypes)
    return selectedUri
}

@Composable
fun ChooseDirFile(playingMusicVm: PlayingMusicVm = hiltViewModel()): List<DocumentFile> {
    val activity = LocalContext.current
    var listFiles by remember { mutableStateOf<List<DocumentFile>>(emptyList()) }
    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { it ->
            val documentF = it?.let { uri -> DocumentFile.fromTreeUri(activity, uri) }
            if (documentF?.isDirectory == true) {
                listFiles = documentF.listFiles().filter { it.isFile }
            }
        }
    Column {
        Button(onClick = {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                launcher.launch(
                    Downloads.EXTERNAL_CONTENT_URI,
                    ActivityOptionsCompat.makeTaskLaunchBehind()
                )
            }
            val map = listFiles.map { file ->
                Song(
                    id = 0,
                    title = file.name ?: "name",
                    icon = "",
                    description = file.uri.encodedPath ?: "encodedPath",
                    artist = "",
                    duration = 60,
                    isVip = true,
                    isTemp = false,
                    path = file.uri.path ?: "path",
                    uri = file.uri.toString() ?: "name"
                )
            }
            playingMusicVm.setSongs(map)
        }) {
            Text("Select Folder")
        }

    }

    return listFiles
}
//Comparable<{String & Uri}>

@Composable
fun ChooseFiles(
    mimetypes: String
): List<Uri> {
    var selectedUris by remember { mutableStateOf<List<Uri>>(emptyList()) }
    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) { it ->
            selectedUris = it
        }
    launcher.launch(mimetypes)
    return selectedUris
}
